﻿using System;
using System.Windows;
using System.Windows.Browser;

namespace UltimateNavigationFramework.UriNavigation
{
    public sealed class UltimateNavigationJournalEntry : DependencyObject
    {
        #region Private fields
        private Uri _source;
        #endregion

        #region Dependency Properties
        public static readonly DependencyProperty NameProperty = DependencyProperty.RegisterAttached("Name",
            typeof(string),
            typeof(UltimateNavigationJournalEntry),
            new PropertyMetadata(new PropertyChangedCallback(UltimateNavigationJournalEntry.NamePropertyChanged)));

        public static readonly DependencyProperty NavigationContextProperty = DependencyProperty.RegisterAttached("NavigationContext",
            typeof(UltimateNavigationContext),
            typeof(UltimateNavigationJournalEntry),
            (PropertyMetadata)null);
        #endregion

        #region Properties
        /// <summary>
        /// Name to map to the title of HTML Page Title
        /// </summary>
        public string Name
        {
            get
            {
                return (string)this.GetValue(UltimateNavigationJournalEntry.NameProperty);
            }
            set
            {
                this.SetValue(UltimateNavigationJournalEntry.NameProperty, (object)value);
            }
        }

        /// <summary>
        /// Prism Navigation Context
        /// </summary>
        public UltimateNavigationContext NavigationContext
        {
            get
            {
                return (UltimateNavigationContext)this.GetValue(UltimateNavigationJournalEntry.NavigationContextProperty);
            }
            set
            {
                this.SetValue(UltimateNavigationJournalEntry.NavigationContextProperty, (object)value);
            }
        }

        /// <summary>
        /// Source Uri (Non Dependency)
        /// </summary>
        public Uri Source
        {
            get
            {
                return this._source;
            }
            set
            {
                if (value != null)
                    this._source = value;
            }
        } 
        #endregion

        #region Constructors
        static UltimateNavigationJournalEntry()
        {
        }

        public UltimateNavigationJournalEntry(string name, Uri uri)
        {
            if (uri != null)
            {
                this.Name = name;
                this._source = uri;
            }
        } 
        #endregion

        #region Get Set Name
        /// <summary>
        /// Get Name
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetName(DependencyObject obj)
        {
            if (obj == null)
                throw new ArgumentNullException("obj");

            return (string)obj.GetValue(UltimateNavigationJournalEntry.NameProperty);
        }

        /// <summary>
        /// Set Name
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="name"></param>
        public static void SetName(DependencyObject obj, string name)
        {
            if (obj == null)
                throw new ArgumentNullException("obj");

            obj.SetValue(UltimateNavigationJournalEntry.NameProperty, (object)name);
        }

        /// <summary>
        /// Name Dependency Property Changed
        /// </summary>
        /// <param name="depObj"></param>
        /// <param name="e"></param>
        private static void NamePropertyChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {
            HtmlPage.Document.SetProperty("title", e.NewValue);
        } 
        #endregion

        #region Get Set Navigation Context
        /// <summary>
        /// Gets the prism navigation context
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static UltimateNavigationContext GetNavigationContext(DependencyObject obj)
        {
            if (obj == null)
                throw new ArgumentNullException("obj");

            return (UltimateNavigationContext)obj.GetValue(UltimateNavigationJournalEntry.NavigationContextProperty);
        }

        /// <summary>
        /// Sets the prism navigation context
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="navigationContext"></param>
        public static void SetNavigationContext(DependencyObject obj, UltimateNavigationContext navigationContext)
        {
            if (obj == null)
                throw new ArgumentNullException("obj");

            obj.SetValue(UltimateNavigationJournalEntry.NavigationContextProperty, (object)navigationContext);
        } 
        #endregion
    }
}
